package com.itany.springcloud.provider.controller;

import com.itany.springcloud.common.entity.User;
import com.itany.springcloud.common.vo.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户服务相关的API接口")
@RestController
@RequestMapping("/users")
public class UserController {

    private static Map<Integer,User> users=new HashMap<Integer,User>();
    private static Integer id=1;

    @ApiImplicitParams({
            @ApiImplicitParam(name="username",value = "用户名",required = true),
            @ApiImplicitParam(name="password",value = "密码",required = true)
    })
    @ApiOperation("添加用户信息")
    @PostMapping
    public ResponseResult add(User user){
        System.out.println("UserController.add");
        user.setId(id++);
        users.put(user.getId(),user);
        return ResponseResult.success();
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name="username",value = "用户名",required = true),
            @ApiImplicitParam(name="password",value = "密码",required = true)
    })
    @ApiOperation("添加用户信息")
    @PostMapping("/add2")
    public ResponseResult add2(@RequestBody  User user){
        System.out.println("UserController.add2");
        user.setId(id++);
        users.put(user.getId(),user);
        return ResponseResult.success();
    }


    @ApiOperation(value = "查询所有用户信息",notes = "备注:这是xxx修改的")
    @GetMapping
    public ResponseResult findAll(){
        System.out.println("UserController.findAll-3");
//        int i=1/0;
        List<User> list=new ArrayList<User>(users.values());
//        return ResponseResult.success(list);
        return ResponseResult.success("OK-3", list);
    }


    @ApiImplicitParam(name = "id",value = "用户编号",dataType = "Integer",required = true)
    @ApiOperation("根据主键查询用户信息")
    @GetMapping("/{id}")
    public ResponseResult findById(@PathVariable("id") Integer id){
        System.out.println("UserController.findById");
        return ResponseResult.success(users.get(id));
    }

    @ApiImplicitParam(name = "id",value = "用户编号",dataType = "Integer",required = true)
    @ApiOperation("删除指定的用户")
    @DeleteMapping("/{id}")
    public ResponseResult remove(@PathVariable("id") Integer id){
        System.out.println("UserController.remove");
        users.remove(id);
        return ResponseResult.success();
    }

    @ApiOperation("修改指定的用户信息")
    @PutMapping
    public ResponseResult modify(User user){
        System.out.println("UserController.modify");
        users.put(user.getId(),user);
        return ResponseResult.success();
    }
}
